﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder;
using ITAP.ShprossesBuilder.Modelling.Components;

namespace ITAP.ShprossesBuilder.Editor
{
    public partial class SelectMaterialForm : Form
    {
        public bool EndsToSpacer
        {
            get
            {
                return checkBoxEndsToSpacer.Checked;
            }
        }

        private List<Material> materials;
        private bool isSucceed = false;

        private double profileLength;

        public double ProfileLength
        {
            get { return profileLength; }
        }

        private IComponent selectedComponent;

        public IComponent SelectedComponent
        {
            get { return selectedComponent; }
        }


        public SelectMaterialForm()
        {
            InitializeComponent();
        }

        public void Initialize(ProfilesProviderDelegate profilesDelegate, Material[] materials, bool showLength, string caption, bool showToSpacerCheckBox)
        {
            checkBoxEndsToSpacer.Visible = showToSpacerCheckBox;
            this.Text = caption;
            if (!showLength)
            {
                this.label1.Visible = false;
                this.numericUpDownProfileLength.Visible = false;
            }
            else
            {
                this.label1.Visible = true;
                this.numericUpDownProfileLength.Visible = true;
            }
            this.materials = new List<Material>(materials);
            this.materialPanel.Initialize(profilesDelegate, new ITAP.ShprossesBuilder.Render.ThumbnailRender(), new Size(GlobalPreferences.Preferences.ThumbnailSize, GlobalPreferences.Preferences.ThumbnailSize), materials, null, null);
            this.isSucceed = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.profileLength = (double)numericUpDownProfileLength.Value;

            if (this.materialPanel.SelectedComponent != null)
            {
                this.selectedComponent = this.materialPanel.SelectedComponent;
                this.isSucceed = true;
                return;
            }
            else
                MessageBox.Show("Не был выбран материал");
        }

        private void SelectShprossForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!(this.DialogResult == DialogResult.Cancel) && !isSucceed)
                e.Cancel = true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxEndsToSpacer.Checked)
            {
                numericUpDownProfileLength.Enabled = false;
                label1.Enabled = false;
            }
            else
            {
                numericUpDownProfileLength.Enabled = true;
                label1.Enabled = true;
            }
        }
    }
}
